import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/ai/ai_pay_entity.dart';
import 'package:ball_lion/entity/match/secret_info_entity.dart';
import 'package:ball_lion/entity/match/secret_price_entity.dart';
import 'package:ball_lion/module/pay/pay_bottom_sheet.dart';
import 'package:ball_lion/server/api/soccer_api.dart';
import 'package:ball_lion/server/api/coupon_api.dart';
import 'package:ball_lion/server/api/pay_api.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';

class SecretLogic extends GetxController with GetTickerProviderStateMixin {
  List<List<SecretInfoEntity>?> secrets = [null, null, null];
  int? roomId = Get.parameters['roomId'].toInt();
  List<String> tabs = ["全部", "未完结", "已完结"];
  List<int> secretIndex = [1, 1, 1];
  List<int> total = [0, 0, 0];
  int _index = 0;
  RightCheck? check;
  bool showTabColor = false;
  late TabController tabController =
      TabController(length: secrets.length, vsync: this);
  EasyRefreshController refreshController =
      EasyRefreshController(controlFinishLoad: true);
  final ScrollController refreshScroll = ApmScrollController();
  int? _findHistory;
  String? price;
  String? realPrice;

  int get index => _index;
  int? get findHistory => _findHistory;

  set findHistory(int? value) {
    _findHistory = value;
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      update();
    });
  }

  set index(int value) {
    _index = value;
    update();
  }

  @override
  void onInit() {
    tabController.addListener(() {
      index = tabController.index;
      getData();
      refreshController.resetFooter();
    });
    refreshScroll.addListener(() {
      if (refreshScroll.offset >= refreshScroll.position.maxScrollExtent &&
          showTabColor == false) {
        showTabColor = true;
        update();
      } else if (refreshScroll.offset <
              refreshScroll.position.maxScrollExtent &&
          showTabColor == true) {
        showTabColor = false;
        update();
      }
    });
    super.onInit();
  }

  @override
  void onReady() {
    getData();
    super.onReady();
  }

  Future getData() async {
    var data = await SoccerApi.getSecretList(index, 1, 15);
    total[index] = data?.total ?? 0;
    secrets[index] = data?.secretList ?? [];
    var result = await PayApi.getSecretPay(roomId: roomId);
    List<SecretPrices>? secretPrice = result.priceList;
    check = RightCheck(
        endTime: result.endTime, isOpen: result.open == true ? 1 : 0);
    if (secretPrice?.length != 0) {
      double p = secretPrice?.first.price ?? 0;
      var coupons = User.isLogin
          ? await CouponApi.use1Coupons(
              gold: p, scene: PayScene.secret, roomId: roomId)
          : [];
      double coupon =
          coupons?.filterOrNull((item) => item.autoSelect == 1)?.first.gold ??
              0;
      realPrice = (p - coupon).toStringAsFixed(2);
      price = p.toStringAsFixed(2);
    }
    update();
  }

  void getRefresh() {
    secretIndex[index] = 1;
    getData();
    refreshController.resetFooter();
    update();
  }

  Future loadMore() async {
    secretIndex[index] += 1;
    var data = await SoccerApi.getSecretList(index, secretIndex[index], 15);
    secrets[index]?.addAll(data?.secretList ?? []);
    refreshController.finishLoad(
        (secrets[index]?.length ?? 0) < secretIndex[index] * 15
            ? IndicatorResult.noMore
            : IndicatorResult.success);
    update();
  }

  void doRefresh() {
    refreshController.callRefresh(scrollController: refreshScroll);
  }
}
